package com.adam.example.aggregation.metric;

import com.adam.example.TransportClientHolder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
import org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder;

import java.net.UnknownHostException;

public class StatsAggregationExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        StatsAggregationBuilder statsAggregationBuilder = AggregationBuilders.stats("agg").field("price");
        SearchResponse searchResponse = transportClient.prepareSearch("test_books")
                .setSize(0).addAggregation(statsAggregationBuilder).get();
        Stats stats = searchResponse.getAggregations().get("agg");
        System.out.println("max=" + stats.getMax());
        System.out.println("min=" + stats.getMin());
        System.out.println("sum=" + stats.getSum());
        System.out.println("avg=" + stats.getAvg());
        System.out.println("count=" + stats.getCount());

        transportClient.close();
    }

}
